package org.cmcc.pcap.jpcap.tcp.tcp.xdr;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.cmcc.pcap.jpcap.tcp.TcpConnection;
import org.cmcc.pcap.jpcap.tcp.TcpPacketDecorator;
import org.cmcc.pcap.jpcap.util.DateUtil;

import com.cmcc.awareness.AppInfo;
import com.cmcc.awareness.ProtoDat;
import com.cmcc.xdr.entity.HttpEntity;
import com.cmcc.xdr.pcapparse.tcp.http.HttpProtoDistinguishStatistics;

import jpcap.packet.TCPPacket;

public class TcpXdrGenerate {
	private static Logger logger = LogManager.getLogger(TcpXdrGenerate.class);
	
	public  ArrayList<HttpEntity> tcpEntityList = new ArrayList<HttpEntity>();
	
	public void generate(TcpConnection tcpConnection) {
		//TcpConnection tcpConnection = httpTransaction.tcpConnection;
		
		logger.debug("this is tcp xdr generate !");
		HttpEntity tcpEntity = new HttpEntity();
		
		//调用业务识别模块需要传入前8个带数据的TcpPacketDecorator
		tcpConnection.reOrderTcpPacket2();
		ArrayList<TCPPacket> altemp = new ArrayList<TCPPacket>();
		Integer altemp_length = 0;
		for(TcpPacketDecorator tpd:tcpConnection.al){
			if(tpd.getData(1)!= null){
				altemp.add(tpd.tcpPacket);
				++altemp_length;
			}
			if(altemp_length == 8)
				break;
		}
		
		ProtoDat protod = ProtoDat.getInstance();
		AppInfo appInfo = null;
		/*    以下调用业务识别模块       */
		for(TCPPacket tp:altemp){
			appInfo = protod.appDistinguish(tp);
			if(appInfo!=null){
				HttpProtoDistinguishStatistics.getInstance().incrementDistinguish(appInfo);
				logger.debug("\n[ match app rule ]"+appInfo.toString());
				
				//填写APP_type
				String appType = appInfo.getNum_type();
				tcpEntity.setAppType(appType);
				
				//填写APP_SUB_type
				String appSubType = appInfo.getNum_subtype();
				tcpEntity.setAppSubType(appSubType);
				
				break;
			}
		}
		if(appInfo == null){
			HttpProtoDistinguishStatistics.getInstance().incrementUnDistinguish();
			logger.debug("\n[ do not match any app rule ]");
		}
		
//		AppInfo appInfo = protod.appDistinguish(altemp);
//		if(appInfo!=null){
//			HttpProtoDistinguishStatistics.getInstance().incrementDistinguish(appInfo);
//			logger.debug("\n[ match app rule ]"+appInfo.toString());
//			
//			//填写APP_type
//			String appType = appInfo.getNum_type();
//			tcpEntity.setAppType(appType);
//			
//			//填写APP_SUB_type
//			String appSubType = appInfo.getNum_subtype();
//			tcpEntity.setAppSubType(appSubType);
//			
//		}else{
//			HttpProtoDistinguishStatistics.getInstance().incrementUnDistinguish();
//			logger.debug("\n[ do not match any app rule ]");
//		}
		
	
		try {
			saveHttpsTcpConnectionToHttpsEntityList(tcpConnection,tcpEntity);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			logger.error(e.getCause(), e);
		}
		
		//将每个httpEntity加入httpEntityList中，调用SaveHttpTransactionInfo的save方法保存到数据库中
		tcpEntityList.add(tcpEntity);
		
		/**
		 * Interface
		 * ProcedureStartTime
		 * ProcedureEndTime
		 * ProtocolType
		 * AppType
		 * AppSubType
		 * IPAddressType
		 * UserIPv4
		 * UserIPv6
		 * ......
		 * 
		 * 
		 */
			
	}
	
	public void saveHttpsTcpConnectionToHttpsEntityList(TcpConnection tcpConnection,HttpEntity tcpEntity) throws ParseException{
		
		//填写IE和PORTAL,默认值是0，必须填写
		tcpEntity.setIe(0);
		tcpEntity.setPortal(0);
		
		Integer length = tcpConnection.al.size();
		logger.debug("tcpConnection length:"+length);
		
		TcpPacketDecorator tpdStart = tcpConnection.al.get(0);
		TcpPacketDecorator tpdEnd = tcpConnection.al.get(length-1);		
		
		//计算procedure_start_time
		long timestamp_procedure_start_time = tpdStart.tcpPacket.sec*1000 + tpdStart.tcpPacket.usec;
		Date procedureStartTime = DateUtil.formatTimestamp(timestamp_procedure_start_time);
		tcpEntity.setProcedureStartTime(procedureStartTime);
				
		//计算procedure_end_time
		long timestamp_procedure_end_time = tpdEnd.tcpPacket.sec*1000 + tpdEnd.tcpPacket.usec;
		Date procedureEndTime = DateUtil.formatTimestamp(timestamp_procedure_end_time);
		tcpEntity.setProcedureEndTime(procedureEndTime);
		
		//填写interface
		Integer interFace = 11;
		tcpEntity.setInterFace(interFace);
		
		//填写ip_address_type,ipv4是0,ipv6是1
		tcpEntity.setIpAddressType(0);
				
		//填写终端用户的ipv4地址
		String userIpv4 = tpdStart.tcpPacket.src_ip.toString();
		userIpv4 = userIpv4.replace("/", "");
		tcpEntity.setUserIpv4(userIpv4);
				
		//填写终端用户ipv6地址
		String userIpv6 = "1111111111111111";
		tcpEntity.setUserIpv6(userIpv6);
					
		//填写终端用户port
		Integer userPort = tpdStart.tcpPacket.src_port;
		tcpEntity.setUserPort(userPort);
					
		//填写L4 Protocol,0为tcp,1为udp
		tcpEntity.setL4Protocal(0);
		
		//填写服务端ipv4地址
		String appServerIpIpv4 = tpdStart.tcpPacket.dst_ip.toString();
		appServerIpIpv4 = appServerIpIpv4.replace("/", "");
		tcpEntity.setAppServerIpIpv4(appServerIpIpv4);
					
		//填写服务端ipv6地址
		String appServerIpIpv6 = "1111111111111111";
		tcpEntity.setAppServerIpIpv6(appServerIpIpv6);
					
		//填写服务端port
		Integer appServerPort = tpdStart.tcpPacket.dst_port;
		tcpEntity.setAppServerPort(appServerPort);

		
//		//计算tcpconnection的统计量指标
//		TcpStatistics tcpstatistic = new TcpStatistics(tcpConnection);
//		tcpstatistic.packetstatistics(tcpConnection);
//		
//		//填写上行流量DL DATA
//		Integer ulData = tcpstatistic.UL_Data;
//		tcpEntity.setUlData(ulData);
//				
//		//填写下行流量UL DATA
//		Integer dlData = tcpstatistic.DL_Data;
//		tcpEntity.setDlData(dlData);
//				
//		//填写上行ip包个数 
//		Integer ulIpPacket = tcpstatistic.UL_IP_Packet;
//		tcpEntity.setUlIpPacket(ulIpPacket);
//				
//		//填写下行ip包个数
//		Integer dlIpPacket = tcpstatistic.DL_IP_Packet;
//		tcpEntity.setDlIpPacket(dlIpPacket);
//				
//		//填写上行tcp乱序包
//		Integer ulDisorderIpPacket = tcpstatistic.UL_Disorder_IP_Packet;
//		tcpEntity.setUlDisorderIpPacket(ulDisorderIpPacket);
//				
//		//填写下行tcp乱序包
//		Integer dlDisorderIpPacket = tcpstatistic.DL_Disorder_IP_Packet;
//		tcpEntity.setDlDisorderIpPacket(dlDisorderIpPacket);
//				
//		//填写上行tcp重传包
//		Integer ulRetransIpPacket = tcpstatistic.UL_Retrans_IP_Packet;
//		tcpEntity.setUlRetransIpPacket(ulRetransIpPacket);
//				
//		//填写下行tcp重传包
//		Integer dlRetransIpPacket = tcpstatistic.DL_Retrans_IP_Packet;
//		tcpEntity.setDlRetransIpPacket(dlRetransIpPacket);
//				
//		//填写上行ip分片数
//		Integer ulIpFragPackets = tcpstatistic.UL_IP_FRAG_PACKETS;
//		tcpEntity.setUlIpFragPackets(ulIpFragPackets);
//				
//		//填写下行ip分片数
//		Integer dlIpFragPackets = tcpstatistic.DL_IP_FRAG_PACKETS;
//		tcpEntity.setDlIpFragPackets(dlIpFragPackets);
//				
//		//填写tcp中syn的次数
//		Integer tcpSynNum = tcpstatistic.TCP_SYN_Num;
//		tcpEntity.setTcpSynNum(tcpSynNum);
//				
//		//填写tcp中syn ack次数
//		Integer tcpSynAckNum = tcpstatistic.TCP_SYN_ACK_Num;
//		tcpEntity.setTcpSynAckNum(tcpSynAckNum);
//				
//		//填写回复syn ack的ack次数
//		Integer tcp-*AckNum = tcpstatistic.TCP_ACK_Num;
//		tcpEntity.setTcpAckNum(tcpAckNum);
		
//		//填写protocol type，tcp是多少？
//		tcpEntity.setProtocolType("2");
		
		
	}
}